package ru.ognick.coachsystem.editor;

import java.io.File;
import java.net.URI;
import java.util.ArrayList;

import ru.ognick.coachsystem.R;
import ru.ognick.coachsystem.common.Training;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.ListView;

public class EditorActivity extends Activity {
	public final String TAG = getClass().getSimpleName();
	
    protected static final int IDD_MEDIA = 101;
    private ListView listView;
    private EditExerciseAdapter exerciseAdapter;
    private Training training;
    protected EditExerciseView exerciseWaitMedia;
    protected ArrayList<Integer> openExercises;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.editor);
        
        listView = (ListView) findViewById(R.id.listViewEditor);
//        listView.setFocusableInTouchMode(false);
//        listView.setSelected(false);
//        listView.requestFocus();
        
        exerciseAdapter = new EditExerciseAdapter(this, 
        		                                  android.R.layout.simple_list_item_1,
        		                                  new ArrayList<EditExerciseView>());
        
        training = (Training) getIntent().getSerializableExtra("TRAINING");
        
        if (savedInstanceState == null)
        	openExercises = new ArrayList<Integer>();
        else {
        	openExercises = savedInstanceState.getIntegerArrayList("OPEN_EXERCISES");
        	if (openExercises == null)
            	openExercises = new ArrayList<Integer>();
        }

        exerciseAdapter.loadData(training);
        exerciseAdapter.restoreOpen();
        listView.setAdapter(exerciseAdapter);
  	  	Log.d(TAG, "onCreate");
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            moveTaskToBack(false);
//            return true;
    		exerciseAdapter.holdData();
        	training.save();
        }
        return super.onKeyDown(keyCode, event);
    }
    
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////ActivityResult//////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
    
	protected void onActivityResult(int requestCode, int resultCode,
			Intent intent) {
		if (requestCode == IDD_MEDIA) {
			if (resultCode == RESULT_OK) {
				Uri uri = intent.getData();
				if (uri != null && exerciseWaitMedia != null) {
					String path = uri.toString();
					Log.d(TAG, "PATH " + uri);
					if (path.toLowerCase().startsWith("file://")) {
						File media = new File(URI.create(path)); 
						path = media.getAbsolutePath();
						exerciseWaitMedia.getExercise().setMediaFile(media);
						exerciseWaitMedia.getMediaButton().setText(path);
						return;
					}
				}
			}
			exerciseWaitMedia.getMediaButton().setText("...");
			exerciseWaitMedia.getExercise().setMediaFile(null);

		}
	}
	

///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////STATE////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////	
	@Override
	public void onSaveInstanceState(Bundle savedInstanceState) {
		exerciseAdapter.holdData();
		savedInstanceState.putIntegerArrayList("OPEN_EXERCISES", openExercises);
		Log.d(TAG, "onSaveInstanceState");
		super.onSaveInstanceState(savedInstanceState);
	}
}
